package com.xique.door.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xique.common.core.text.Convert;
import com.xique.common.core.utils.DateUtils;
import com.xique.common.core.utils.SecurityUtils;
import com.xique.common.core.utils.StringUtils;
import com.xique.door.bean.domain.BuildArea;
import com.xique.door.bean.domain.BuildAreaV1;
import com.xique.door.bean.search.build.AreaListSearch;
import com.xique.door.constant.BuildConstant;
import com.xique.door.mapper.BuildAreaMapper;
import com.xique.door.mapper.BuildAreaMapperV1;
import com.xique.door.service.IBuildAreaService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 区域Service业务层处理
 *
 * @author shd
 * @date 2021-09-09
 */
@Service
public class BuildAreaServiceImpl extends ServiceImpl<BuildAreaMapperV1, BuildAreaV1> implements IBuildAreaService
{
    @Autowired
    private BuildAreaMapper buildAreaMapper;

    @Autowired
    private BuildAreaMapperV1 areaMapperV1;

    /**
     * 查询区域
     *
     * @param id 区域ID
     * @return 区域
     */
    @Override
    public BuildArea selectBuildAreaById(Long id)
    {
        return buildAreaMapper.selectBuildAreaById(id);
    }

    /**
     * 查询区域列表
     *
     * @param buildArea 区域
     * @return 区域
     */
    @Override
    public List<BuildArea> selectBuildAreaList(BuildArea buildArea)
    {
        buildArea.setDelFlag(BuildConstant.BuildDelFlag.EXIST);
        return buildAreaMapper.selectBuildAreaList(buildArea);
    }

    /**
     * 新增区域
     *
     * @param buildArea 区域
     * @return 结果
     */
    @Override
    public int insertBuildArea(BuildArea buildArea)
    {
        buildArea.setCreateTime(DateUtils.getNowDate());
        buildArea.setUpdateTime(buildArea.getCreateTime());
        buildArea.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
        buildArea.setUpdateBy(buildArea.getCreateBy());
        return buildAreaMapper.insertBuildArea(buildArea);
    }

    /**
     * 修改区域
     *
     * @param buildArea 区域
     * @return 结果
     */
    @Override
    public int updateBuildArea(BuildArea buildArea)
    {
        buildArea.setUpdateTime(DateUtils.getNowDate());
        buildArea.setUpdateBy(String.valueOf(SecurityUtils.getUserId()));
        return buildAreaMapper.updateBuildArea(buildArea);
    }

    /**
     * 删除区域对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteBuildAreaByIds(String ids)
    {
        return buildAreaMapper.deleteBuildAreaByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除区域信息
     *
     * @param id 区域ID
     * @return 结果
     */
    @Override
    public int deleteBuildAreaById(Long id)
    {
        return buildAreaMapper.deleteBuildAreaById(id);
    }

    @Override
    public IPage<BuildAreaV1> selectAreaPageV1(AreaListSearch search) {

        QueryWrapper<BuildAreaV1> condition = new QueryWrapper<>();
        if (StringUtils.isNotNull(search.getParkId())) {
            condition.eq("park_id", search.getParkId());
        }
        condition.eq("del_flag", "0");
        Page<BuildAreaV1> page = new Page<>();
        if (StringUtils.isNotNull(search.getPageNum()) && StringUtils.isNotNull(search.getPageSize())) {
            page.setCurrent(search.getPageNum());
            page.setSize(search.getPageSize());
        }
        condition.orderByAsc("create_time");
        return areaMapperV1.selectPage(page, condition);
    }

    @Override
    public List<BuildAreaV1> selectAreaListV1(AreaListSearch search) {
        QueryWrapper<BuildAreaV1> condition = new QueryWrapper<>();
        if (StringUtils.isNotNull(search.getParkId())) {
            condition.eq("park_id", search.getParkId());
        }
        if (CollectionUtils.isNotEmpty(search.getIds())) {
            condition.in("id", search.getIds());
        }
        condition.eq("del_flag", "0");
        condition.orderByAsc("create_time");
        return areaMapperV1.selectList(condition);
    }

    @Override
    public BuildAreaV1 selectAreaV1(BuildAreaV1 buildAreaV1) {
        QueryWrapper<BuildAreaV1> condition = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(buildAreaV1.getAreaName())) {
            condition.in("area_name", buildAreaV1.getAreaName());
        }
        if (StringUtils.isNotNull(buildAreaV1.getParkId())) {
            condition.eq("park_id", buildAreaV1.getParkId());
        }
        condition.eq("del_flag", "0");
        condition.orderByAsc("create_time");
        return areaMapperV1.selectOne(condition);
    }
    public  String getCode(Long parkId){
        QueryWrapper<BuildAreaV1> condition = new QueryWrapper<>();
        condition.select("(max(code)+1) as maxCode");
        if (StringUtils.isNotNull(parkId)) {
            condition.eq("park_id", parkId);
        }
        BuildAreaV1 buildAreaV1=areaMapperV1.selectOne(condition);
        if(buildAreaV1==null){
            return "1";
        }
        return  String.valueOf(buildAreaV1.getMaxCode());
    }
}
